package com.xingbg.projectimpl.config.controller;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.mvc.condition.RequestCondition;

import javax.servlet.http.HttpServletRequest;

/**
 * @author yuqi138
 * @version 1.0
 * @Description:对RequestMappingHandlerMapping的匹配过程进行定制，则需要为每个注册的RequestMappingInfo注册一个RequestCondition对象
 * @date 2021/5/26 15:38
 * @Copyright: 2021 www.jddglobal.com|www.haiyi-info.com Inc.All rights reserved.
 */
// 由于每个RequestMappingInfo中都会持有一个RequestCondition对象，并且这些对象都是有状态的，
// 因而这里必须为其使用prototype进行标注，表示每次从BeanFactory中获取该对象时都会创建一个新的对象
@Data
@Slf4j
public class LiskovRequestCondition implements RequestCondition<LiskovRequestCondition> {

    /**
     * 是否在子类重写的jar中
     */
    private Boolean isChildJar;

    public LiskovRequestCondition(){}

    public LiskovRequestCondition(Boolean isChildJar) {
        this.isChildJar = isChildJar;
    }

    @Override
    public LiskovRequestCondition combine(LiskovRequestCondition other) {
        return new LiskovRequestCondition(other.isChildJar);//(other.codeVersion);
    }

    @Override
    public LiskovRequestCondition getMatchingCondition(HttpServletRequest request) {
        return this;
    }

    @Override
    public int compareTo(LiskovRequestCondition other, HttpServletRequest request) {

        int thisOrder =  Boolean.TRUE.equals(this.isChildJar)?1:-1;
        int otherOrder = Boolean.TRUE.equals(other.isChildJar)?1:-1;
        if (thisOrder==otherOrder) {
            throw new RuntimeException("不允许在相同jar包中包含相同的url路径。");
        }
        return otherOrder - thisOrder;
    }
}
